package com.fjec.paid.controller;

import com.fjec.common.enums.EnumSysCode;
import com.fjec.common.protocol.JsonResult;
import com.fjec.common.ys.PaidHelper;
import com.fjec.common.ys.protocol.ResponsePaid;
import com.fjec.paid.rabbitmq.provider.YsPayDelayQueryProvoder;
import com.fjec.paid.service.PhysicalPreparePayService;
import org.apache.commons.collections.MapUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.Map;

@RestController
@RequestMapping("/")
public class PhysicalPreparePayController {

    @Resource
    private PhysicalPreparePayService physicalPreparePayService;

    @Autowired
    private YsPayDelayQueryProvoder ysPayDelayQueryProvoder;

    @Autowired
    private PaidHelper paidHelper;

    /**
     * 用户结算订单后，异步获取prepare pay info
     * @param orderId
     * @return
     */
    @GetMapping("/y/get/prepare/pay/package")
    public JsonResult getPreparePayPackage(Long orderId) {
        Map<String, Object> result = physicalPreparePayService.getPreparePayPackage(orderId);
        if (result == null)
            return JsonResult.FAILURE;
        Integer paidStatus = MapUtils.getInteger(result, "paidStatus");
        if (paidStatus == 2)
            return JsonResult.failure()
                    .code(EnumSysCode.ERR_3024.getCode())
                    .message(EnumSysCode.ERR_3024.getMsg())
                    .build();
        Map<String, Object> data = MapUtils.getMap(result, "ysResponse");
        // 给前端返回预支付信息之前，发送 支付结果查询 mq
        ysPayDelayQueryProvoder.sendYsPayDelayQuery(String.valueOf(orderId), 10);
        ysPayDelayQueryProvoder.sendYsPayDelayQuery(String.valueOf(orderId), 20);
        ysPayDelayQueryProvoder.sendYsPayDelayQuery(String.valueOf(orderId), 30);
        ysPayDelayQueryProvoder.sendYsPayDelayQuery(String.valueOf(orderId), 60);
        ysPayDelayQueryProvoder.sendYsPayDelayQuery(String.valueOf(orderId), 5 * 60);
        ysPayDelayQueryProvoder.sendYsPayDelayQuery(String.valueOf(orderId), 30 * 60);
        ysPayDelayQueryProvoder.sendYsPayDelayQuery(String.valueOf(orderId), 90 * 60);
        return JsonResult.success().data(data).build();
    }


    /**
     * 前端通知后端订单支付完成
     * @param orderId
     * @return
     */
    @PostMapping("/y/pay/order/successfully")
    public JsonResult payOrderSuccessfully(Long orderId) {
        // 前端通知后端支付完成，发送 支付查询 mq
        ysPayDelayQueryProvoder.sendYsPayDelayQuery(String.valueOf(orderId), 1);
        return JsonResult.SUCCESS;
    }

    @GetMapping("/testPaymentQuery")
    public String test(String outTradeNo, String tradeNo, String shopDate) throws Exception {
        Map<String, Object> res = paidHelper.queryPayment(outTradeNo, tradeNo, shopDate);
        return "ok";
    }
}
